#include <iostream>
using namespace std;
class Data{
public:
    Data()
    {
        cout<<"Data"<<endl;
    }
    ~Data(){
        cout<<"~Data"<<endl;
    }
    void display(){
        cout<<"10"<<endl;
    }
private:
    int data=10;

};

class middlelayer{
public:
    middlelayer(Data * ml)
    :_ml(ml)
    {
        cout<<"middlelayer"<<endl;
    }
    ~middlelayer(){
        if(_ml){
        cout<<"~middlelayer"<<endl;
        delete _ml;
        _ml=nullptr;
    }
    }
    Data* get(){

        return _ml;
    }
    Data * operator->(){
        return _ml;
    }
    Data & operator*(){
        return *_ml;
    }
    void display(){
        cout<<"middlelayer::display"<<endl;
    }
private:
    Data* _ml;
};
class thirdlayer{
public:
    thirdlayer(middlelayer * tl)
    :_tl(tl)
    {
        cout<<"thirdlayer"<<endl;
    }
    ~thirdlayer(){
        if(_tl){
        cout<<"~thirdlayer"<<endl;
        delete _tl;
        _tl=nullptr;
    }
    }
    middlelayer * get(){
        return _tl;
    }
    middlelayer * operator->(){
        return _tl;
    }
    middlelayer & operator*(){
        return *_tl;
    }
    void display(){
        cout<<"thirdlayer::display"<<endl;
    }
private:
middlelayer * _tl;

};

void test(){
    thirdlayer tl( new middlelayer(new Data()));
    tl.get()->get()->display();
    tl->display();
    tl.operator->()->operator->()->display();
}

int main(int argc, char * argv[]){
    test();
    return 0;
}

